<?php

namespace App\Model\Admin;
use Util\Common;
use App\Model\Model;
/**
 * 菜单-模型
 */
class Menus extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    public bool $timestamps = true;

    protected ?string $table = 'menus';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = [
        'id', 'name','component','url','icon','pid','is_del','label','sort','status'
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected array $hidden = [
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected array $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];
    public function getChild(&$data,$type='admin',$arr=[]){
        foreach($data as $k => $v){
            if($type == 'admin'){
                $data[$k]['children']= $this->where('pid',$v['id'])
                    ->where('is_del', Common::DEL_SHOW)
                    ->where('status',Common::DEL_SHOW)
                    ->orderBy('sort','desc')
                    ->get(['id','name','pid','url','icon','component','status','label','sort'])
                    ->toArray();
            }else{
                $data[$k]['children']= Menus::where('pid',$v['id'])
                    ->where('is_del', Common::DEL_SHOW)
                    ->where('status',Common::DEL_SHOW)
                    ->whereIn('id',$arr)
                    ->orderBy('sort','desc')
                    ->get(['id','name','pid','url','icon','component','status','label','sort'])
                    ->toArray();
            }

            if(count($data[$k]['children'])>0){
                $this->getChild($data[$k]['children']);
            }
        }
        return $data;
    }
}
